﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;




namespace ERP.Manager.DataHelper
{
    public class MySqlHelper
    {

        public static MySqlConnection getmysqlcon(string ip, string Uname, string password, string DateBase)
        {
            string M_str_sqlcon = "server=" + ip + ";user id=" + Uname + ";password=" + password + ";database=" + DateBase; //根据自己的设置
            MySqlConnection myCon = new MySqlConnection(M_str_sqlcon);
            return myCon;
        }


        public static MySqlConnection getmysqlcon() 
        {
            string M_str_sqlcon = "server=localhost;user id=root;password=;database=mysqltest"; //根据自己的设置
            MySqlConnection myCon = new MySqlConnection(M_str_sqlcon);
            return myCon;
        }


        public static void PrepareCommand(MySqlCommand cmd, MySqlConnection con, CommandType cmdType, string cmdText, MySqlParameter[] parames)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            cmd.Connection = con;
            cmd.CommandType = cmdType;
            cmd.CommandText = cmdText;

            if (parames != null)
            {
                cmd.Parameters.AddRange(parames);   
            }
        }



        public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params MySqlParameter[] parames)
        {


            MySqlConnection mysqlcon = getmysqlcon();
            MySqlCommand mysqlcom = new MySqlCommand();
            try
            {
                PrepareCommand(mysqlcom, mysqlcon, cmdType, cmdText, parames);
                int result= mysqlcom.ExecuteNonQuery();
                mysqlcom.Parameters.Clear();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                mysqlcom.Dispose();
                mysqlcon.Close();
            }
        }


        public static MySqlDataReader getmysqlread(string M_str_sqlstr, string ip, string Uname, string password, string DateBase)
        {

            MySqlConnection mysqlcon = getmysqlcon(ip, Uname, password, DateBase);
            MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon);
            try
            {
                mysqlcon.Open();
                MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection);
                return mysqlread;
            }
            catch (Exception)
            {
                mysqlcon.Close();
                return null;
            }

        }


        public static MySqlDataReader getmysqlread(string M_str_sqlstr)
        {

            MySqlConnection mysqlcon = getmysqlcon();
            MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon);
            try
            {
                mysqlcon.Open();
                MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection);
                return mysqlread;
            }
            catch (Exception)
            {
                mysqlcon.Close();
                return null;
            }

        }

    }
}
